/*
 * FCKeditor - The text editor for internet
 * Copyright (C) 2003 Frederico Caldeira Knabben
 *
 * Licensed under the terms of the GNU Lesser General Public License
 * (http://www.opensource.org/licenses/lgpl-license.php)
 *
 * For further information go to http://www.fredck.com/FCKeditor/ 
 * or contact fckeditor@fredck.com.
 *
 * fck_toolbar.js: Creates and handles the toolbar.
 *
 * Authors:
 *   Frederico Caldeira Knabben (fckeditor@fredck.com)
 */

//##
//## Command Type Enum
//##
TBCMD_DEC		= 0 ;
TBCMD_DOC		= 1 ;
TBCMD_CUSTOM	= 2 ;

// -----------------------------------------------------------------
// -- TBToolbar class - The Main (upper level) toolbar object.
// -----------------------------------------------------------------
function TBToolbar()
{
	this.Bands = new TBBandList() ;					// The Toolbar Bands collection.
}
TBToolbar.prototype.GetHTML			= TBToolbar_GetHTML ;
TBToolbar.prototype.LoadButtonsSet	= TBToolbar_LoadButtonsSet ;

function TBToolbar_GetHTML() 
{
	var sHTML = '<TABLE width="100%" bgcolor="#EFEFDE" cellspacing="0" cellpadding="0" border="0" unselectable="on"><TR><TD>' ;
	var oBand ;
	
	for (iBand = 0 ; iBand < this.Bands.Array.length ; iBand++) 
	{
		sHTML += '<TABLE class="Toolbar" cellspacing="0" cellpadding="0" border="0" unselectable="on"><TR>' ;
		
		oBand = this.Bands.Array[iBand]
		for (iItem = 0 ; iItem < oBand.Items.Array.length ; iItem++)
		{
			sHTML += '<TD>' + oBand.Items.Array[iItem].GetHTML() + '</TD>' ;
		}
		
		sHTML += '</TR></TABLE>' ;
	}
	
	return '</TD></TR></TABLE>' + sHTML ;
}

function TBToolbar_LoadButtonsSet(toolbarSetName) 
{
	var ToolbarSet = config.ToolbarSets[toolbarSetName] ;
	
	this.Bands = new TBBandList() ;
	var oBand ;
	var sItem ;
	
	for (iBand = 0 ; iBand < ToolbarSet.length ; iBand++) 
	{
		oBand = this.Bands.Add() ;
		for (iItem in ToolbarSet[iBand]) 
		{
			sItem = ToolbarSet[iBand][iItem] ;
			if ((sItem) == '-')
				oBand.Items.Add(new TBSeparator()) ;
			else
				oBand.Items.Add(oTB_Items[sItem]) ;
		}
	}
}

// -----------------------------------------------------------------
// -- TBBandList class - A collection of toolbar bands.
// -----------------------------------------------------------------
function TBBandList() 
{
	this.Array = new Array() ;
} 
TBBandList.prototype.Add = TBBandList_Add ;		// Adds a Band to the collection.

function TBBandList_Add(bandName) 
{
	var i = this.Array.length ;
	this.Array[i] = new TBBand(bandName) ;
	return this.Array[i] ;
}

// -----------------------------------------------------------------
// -- TBBand class - A toolbar band. It holds a group of items (buttons, combos, etc...).
// -----------------------------------------------------------------
function TBBand(bandName) 
{
	this.Name	= bandName || "" ;				// The Band name
	this.Items	= new TBItemList() ;				// The Band Items collection.
}

// -----------------------------------------------------------------
// -- TBItemList class - A collection of items (TBButton's, TBCombo's, etc...).
// -----------------------------------------------------------------
function TBItemList() 
{
	this.Array = new Array() ;
}
TBItemList.prototype.Add = TBItemList_Add ;		// Adds an Item to the collection.

function TBItemList_Add(objectItem)
{
	var i = this.Array.length ;
	this.Array[i] = objectItem ;
	return this.Array[i] ;
}

// -----------------------------------------------------------------
// -- TBButton class - Represents a toolbar button.
// -----------------------------------------------------------------
function TBButton(name, toolTip, command, commandType, width, height)
{
	if (! commandType) commandType = TBCMD_DEC ;
	
	this.Name		= name.toLowerCase() ;
	this.ToolTip	= toolTip ;
	this.Width		= width  || 23 ;
	this.Height		= height || 22 ;
	this.CommandType = commandType || TBCMD_DEC ;
	this.Image		= null ;	
	
	if (command)
	{
		switch (this.CommandType)
		{
			case TBCMD_DEC :
				this.Command     = "decCommand(" + command + ")" ;
				this.CommandId   = command ;
				events.attachEvent('onEditing', this) ;
				break ;
			case TBCMD_DOC :
				this.Command     = "docCommand('" + command + "')" ;
				this.CommandCode = command ;
				events.attachEvent('onEditing', this) ;
				break ;
			default :
				this.Command     = command ;
				break ;
		}
	}
	else
		this.Command = "void(0)" ;
}
TBButton.prototype.GetHTML		= TBButton_GetHTML ;
TBButton.prototype.onEditing	= TBButton_onEditing ;

function TBButton_GetHTML() 
{
	this.OverImage = new Image() ;			// The cached rollover image (for fast viewing response)
	this.OverImage.src = config.ToolbarImagesPath + 'over.button.' + this.Name + '.gif' ;
	return '<IMG id="btn' + this.Name + '" src="' + config.ToolbarImagesPath + 'button.' + this.Name + '.gif" width=' + this.Width 
			+ ' height=' + this.Height 
			+ ' onclick="' + this.Command + '"'
			+ ' onmouseover="TBButtonOver(this,\'' + this.Name + '\');"'
			+ ' onmouseout="TBButtonOut(this,\'' + this.Name + '\');"'
			+ ' alt="' + this.ToolTip + '">' ;
}

function TBButton_onEditing() 
{
	if (this.Image == null) this.Image = document.getElementById('btn' + this.Name) ;
	if (this.Image)
	{
		if (this.Image.BackupOnMouseOver == null) this.Image.BackupOnMouseOver = this.Image.onmouseout ;
		
		if ( (this.CommandType == TBCMD_DEC && checkDecCommand(this.CommandId)) 
			|| (this.CommandType == TBCMD_DOC && checkDocCommand(this.CommandCode)) )
		{
			if (this.Image.onmouseout == null)
				this.Image.onmouseout = this.Image.BackupOnMouseOver ;
			TBButtonOut(this.Image, this.Name) ;
		}
		else
		{
			this.Image.onmouseout = null ;
			TBButtonOver(this.Image, this.Name) ;
		}
	}
}

// -----------------------------------------------------------------
// -- TBCombo class - Represents a toolbar combo.
// -----------------------------------------------------------------
function TBCombo(name, command, label, options, values, onEditingAction)
{
	this.Name    = name ;
	this.Label   = label || "" ;
	this.Command = command ;
	this.Options = options ? options.split(';') : new Array() ;
	this.Values  = values  ? values.split(';')  : new Array() ;

	if (onEditingAction)
	{
		this.onEditingAction = onEditingAction ;
		events.attachEvent('onEditing', this) ;
	}
}
TBCombo.prototype.GetHTML   = TBCombo_GetHTML ;
TBCombo.prototype.onEditing = TBCombo_onEditing ;

function TBCombo_GetHTML() 
{
	var sHTML = this.Label + '&nbsp;\n<SELECT id="cmb' + this.Name + '" onchange="' + this.Command + '">\n' ;
	for (i in this.Options) 
	{
		if (this.Values.length > 0) 
			sHTML += '<OPTION value="' + this.Values[i] + '">' + this.Options[i] + '</OPTION>\n' ;
		else
			sHTML += '<OPTION>' + this.Options[i] + '</OPTION>\n' ;
	}
	return sHTML + '</SELECT>' ;
}

function TBCombo_onEditing() 
{
	eval(this.onEditingAction) ;
}

// -----------------------------------------------------------------
// -- TBCheckBox class - Represents a toolbar checkbox.
// -----------------------------------------------------------------
function TBCheckBox(name, command, label)
{
	this.Name		= name ;
	this.Command	= command ;
	this.Label		= label || "" ;
}
TBCheckBox.prototype.GetHTML = TBCheckBox_GetHTML ;

function TBCheckBox_GetHTML() 
{
	return this.Label + '<INPUT type=checkbox onclick="' + this.Command + '">' ;
}

// -----------------------------------------------------------------
// -- TBSeparator class - Represents a toolbar items separator.
// -----------------------------------------------------------------
function TBSeparator() 
{
	this.Name = '' ;
}
TBSeparator.prototype.GetHTML = TBSeparator_GetHTML ;

function TBSeparator_GetHTML() 
{
	return '<IMG class="spacer" src="' + config.ToolbarImagesPath + 'separator.gif" width=5 height=22>' ;
}

// Behaviours
function TBButtonOver(oImage, Name) 
{
	oImage.src = config.ToolbarImagesPath + 'over.button.' + Name + '.gif'; 
}

function TBButtonOut(oImage, Name)	
{
	oImage.src = config.ToolbarImagesPath + 'button.' + Name + '.gif'; 
}